
function fillChat(chatId){
    if(chat_refresh == true){
        new_msg = false;
        $("#chat_id_input").val(chatId);
        $("#user_id_input").val(userid);
        $("#chatarea").show();
        $("#send_msg").show();
        $("#chat_list").html("");
        var tmp = $("<div>").html("back");
        $(tmp).click(function(){
            $("#chatarea").html("");
            msg_ids = [];
            getChatList(); 
        });
        $("#chat_list").append(tmp);


        $.getJSON('../chat/'+chatId ,function(data){
            console.log("test");
            if(data.valid == true){
                if(chat_refresh == true){
                    chat = $("<div id='chatarea'>");
                    new_ids = [];
                    $.each(data.messages, function(key, val) {
                        var name = $('<label class="chatusername">').html(val.sender_email +": ");
                        var msg_part = $('<span class="chatmsg">').html(val.message);
                        var msg = $('<div id="msg_'+val._id.$id+'">').append(name).append(msg_part);
                        if (val.sender_id == userid){
                            $(msg).addClass("chat_msg_own");
                        }else{
                            $(msg).addClass("chat_msg_other");
                        }
                        new_ids.push(val._id.$id);
                        
                        $(chat).append(msg);
                        if($.inArray(val._id.$id, msg_ids) == -1){
                            new_msg = true; 
                        }
                    });
                    msg_ids = new_ids;
                    if(new_msg == true){
                        $("#chatarea").replaceWith(chat);
                    }
                }
            }else{
                $("#chatarea").html(data.error);
            }
        });
 
        setTimeout(function()
        { 
            fillChat(chatId); 
        }, 1000);
    }
}
      
function sendMessage(){
    $.post('../send_message',$("#save_chat").serialize(), function(data) {  
        });
    
    $('#msg').val(''); 
}      

function getChatList(){
    chat_refresh = false;
    $("#chatarea").hide();
    $("#send_msg").hide();
    $.getJSON('../chat_list/'+userid ,function(data){
        //alert(data);
        if(data.valid == true){
            // alert(data.ids);
            $("#chat_list").html("");
            $.each(data.chats, function(key, val) {
                var row = $('<div class="chat_list_row">');
                var chat = $('<div class="chat_list_names">');
                var remove = $("<div class='remove'>");
                
                $.each(val.users, function(key2, val2) {
                    var email = $('<span>').html(val2.email+" ");
                    $(chat).append(email);
                                        
                });
                        
                $(remove).click(function(){
                    $.getJSON('../remove_chat/'+val.id ,function(data){
                                
                        });   
                    getChatList();
                });   
                //  $(chat).append(remove);
                $(row).append(chat).append(remove);
     
                $(chat).click(function(){
                    chat_refresh = true;
                    fillChat(val.id); 
                });
                $("#chat_list").append(row);
            });
        }else{
            $("#chat_list").html(data.error);
        }
    });
}
            
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
            
function setlogin(){
    $("#chat").show();
    $("#logout").show();
    $("#user_id_chat").val(userid);
    getChatList();
    $("#send_msg").hide();
    $("#menu").show();
    $("#search").show();
}
            
function renderPage(){
    var page = getUrlVars()["page"];
    if(typeof(page) == 'undefined'){
        page = "personify";
    }
    if(login == false){
        $("#chat").hide();
        $("#menu").hide();
        $("#search").hide();
        if(page == "registreren"){
            page = "registreren";
        }else{
            page = "login";
        }
    }
    else {
        if(page == "profile") {
            page = "profile";
        }
        else if(page == "personify") {
            page = "personify";
        }
    }

    $.ajax({
        url: page + ".html",
        cache: false
    }).done(function(data){
       $('#content').html(data);
    });
                
}
